package com.storm.lib.template;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.mina.core.session.IoSession;
import org.springframework.stereotype.Controller;

import com.storm.lib.po.BaseRolePo;
import com.storm.lib.vo.BaseMapAvatarVo;
@Controller
public class RoleTemplate {
	/**
	 * roleId和Avator的关系  
	 */
	public static ConcurrentHashMap<Integer, BaseMapAvatarVo> roleIdAvatorMapping = new ConcurrentHashMap<Integer, BaseMapAvatarVo>();


	/**
	 * iuid和角色id的关系 key=角色id value=iuid
	 */
	public static ConcurrentHashMap<Integer, String> roleIdIuidMapping = new ConcurrentHashMap<Integer, String>();

	/**
	 * iuid和session的关系 key=iuid value=IoSession
	 */
	
	public static ConcurrentHashMap<String, IoSession> iuidSessionMapping = new ConcurrentHashMap<String, IoSession>();
	
	/**
	 * 
	 * 方法功能:insert
	 * 更新时间:2011-8-7, 作者:johnny
	 */
	public void insertIuidSessionMapping(String iuid,IoSession ioSession){
		iuidSessionMapping.put(iuid, ioSession);
	}
	
	

	/**
	 * 
	 * 方法功能:get session
	 * 更新时间:2011-8-7, 作者:johnny
	 * @param iuid
	 * @return
	 */
	public IoSession getSessionByIuid(String iuid){
		if(iuid==null){
			return null;
		}
		return iuidSessionMapping.get(iuid);
	}
	/**
	 * 方法功能:getSessionByRoleId
	 * @author future 2011-10-25
	 * @param roleId
	 * @return
	 */
	public IoSession getSessionById(Integer roleId){
		if(null==roleId){
			return null;
		}
		String iuid = roleIdIuidMapping.get(roleId);
		if(null==iuid){
			return null;
		}
		return iuidSessionMapping.get(iuid);
	}
	
	/**
	 * 
	 * 方法功能:get session
	 * 更新时间:2011-8-7, 作者:johnny
	 * @param iuid
	 * @return
	 */
	public Collection<IoSession> getAllSession(){
		return iuidSessionMapping.values();
	}

	public void insertRoleIdAvatarMapping(BaseRolePo rolePo) {
    	BaseMapAvatarVo mapAvatar=null;
    	mapAvatar.syncValueOrBuild(rolePo);
    	roleIdAvatorMapping.put(rolePo.getId(), mapAvatar);    
	}
	
	public void updateRoleIdAvatarMapping(BaseRolePo rolePo){
    	BaseMapAvatarVo mapAvatar = roleIdAvatorMapping.get(rolePo.getId());
    	mapAvatar.syncValueOrBuild(rolePo);
	}

	public void insertRoleIdIuidMapping(Integer id, String iuid) {
		roleIdIuidMapping.put(id, iuid);
	}
	
	public void removeRoleIdIuidMapping(Integer id) {
		roleIdIuidMapping.remove(id);
	}
}
